$(function() {
	var tr = $(".border table tr").not(":first");
	tr.hover(function() {
		$(this).addClass("tr-active");
	},function() {
		$(this).removeClass("tr-active");
	})
})

function deletePosition(obj, id) {
	layer.confirm('是否要删除该职位？', {
	   btn: ['确定','取消']
	}, function(){
		var params = {
			'position.id' : id,
			date: new Date().getTime()
		}
		$.get(window.ctx + '/company/Company/deletePosition.action', params, function(result) {
			if (result === '1') {
				
				$(obj).parent().parent().parent().remove();
				layer.msg('该职位删除成功！', {icon: 6});
			} else {
				layer.msg('该职位删除失败！', {icon: 5});
			}
		})
	});
}

function delay(obj, id) {
	var validDays = $(obj).data('default-days');
	validDays = parseInt(validDays);
	
	var $delayValidDay = $('#delayValidDay');
	var $validDay = $('#validDay');

	$delayValidDay.val(validDays);
	$validDay.html(validDays);
	layer.open({
		title:'设置延期时间',
		type: 1,
		area: ['500px'],
		btn:["确认","取消"],
		content: $('#modal'),
		yes:function(index){
			var delayDay = parseInt($('[name="delayDay"]:checked').val());
			var params = {
				'position.id': id,
				delayDay: delayDay,
				date: new Date().getTime()
			}
			$.get(window.ctx + '/company/Company/delay.action', params, function(result) {
				if (result === '1') {
					// 更新页面上的时间
					var dateTd = $(obj).parent().parent().prev();
					var dateStr = dateTd.html();
					// 字符串 -> 时间
					var strs = dateStr.split('-');
					var year = strs[0];
					var month = strs[1];
					var day = strs[2];
					var date = new Date();
					date.setFullYear(year);
					date.setMonth(month - 1);
					date.setDate(day);
					// 延期一定天数
					var afterTime = date.getTime() + delayDay * 24 * 60 * 60 * 1000;
					date.setTime(afterTime);
					// 时间 -> 字符串
					var flushDateStr = '';
					flushDateStr += date.getFullYear();
					flushDateStr += ('-' + formatNumber(date.getMonth() + 1));
					flushDateStr += ('-' + formatNumber(date.getDate()));
					dateTd.html(flushDateStr);
					function formatNumber(number) {
						return number >= 10 ? number : '0' + number;
					}
					
					// 更新data-default-days的值
					var newValidDays = validDays + delayDay;
					$(obj).data('default-days', newValidDays)
					
					// 更新模态框里的有效期
					$delayValidDay.val(newValidDays);
					$validDay.html(newValidDays);
					
					layer.msg('该职位已成功延期' + delayDay + '天！', {icon: 6});
				} else {
					layer.msg('该职位延期失败！', {icon: 5});
				}
			})
			layer.close(index);
		}
	});
}


function batchDelay() {
	layer.confirm('默认会以对应职位的有效期进行延期，<br>请确认是否进行批量延期？', {
	   btn: ['确定','取消']
	}, function(){
		var values = [];
		$('.checkbox:checked').each(function(index, element) {
			// 格式： 1, 14, 2017-08-08
			values.push(element.value);
		})
		$.ajax({
			type: 'post',
			url: window.ctx + '/company/Position-batchDelay.action',
			data: {positionCheckedInfo: values},
			traditional: true,
			dataType: 'json',
			success: function(results) {
				for (var i = 0; i < results.length; i++) {
					// 格式： 1, 14, 2017-08-08
					var infos = results[i].split(',');
					var id = infos[0];
					var validDay = infos[1];
					var outDate = infos[2];
					$('#outDateBindId' + id).html(outDate);
					$('#checkboxBindId' + id).val(id + ',' + validDay + ',' + outDate);
					$('#delayLinkBindId' + id).data('default-days', validDay);
				}
				
				layer.msg('批量延期操作成功！', {icon: 6});
			}
		})
	});
}
